package com.wyh.rabbitextensionsdemo.config;

import com.wyh.rabbitextensionsdemo.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DlConfig {
    @Bean("normalQueue")
    public Queue normalQueue(){
        return QueueBuilder.durable(Constants.NORMAL_QUEUE)
                .deadLetterExchange(Constants.DL_EXCHANGE)
                .deadLetterRoutingKey(Constants.DL_ROUTING_KEY)
                .maxLength(2)
                .build();
    }

    @Bean("normalExchange")
    public DirectExchange normalExchange(){
        return ExchangeBuilder.directExchange(Constants.NORMAL_EXCHANGE).build();
    }

    @Bean("normalQueueBinding2")
    public Binding normalQueueBinding2(@Qualifier("normalExchange") DirectExchange exchange,
                                       @Qualifier("normalQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(Constants.NORMAL_ROUTING_KEY);
    }

    @Bean("dlQueue")
    public Queue dlQueue(){
        return QueueBuilder.durable(Constants.DL_QUEUE).build();
    }

    @Bean("dlExchange")
    public DirectExchange dlExchange(){
        return ExchangeBuilder.directExchange(Constants.DL_EXCHANGE).build();
    }

    @Bean("dlQueueBinding2")
    public Binding dlQueueBinding2(@Qualifier("dlExchange") DirectExchange exchange,
                                   @Qualifier("dlQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(Constants.DL_ROUTING_KEY);
    }
}
